

#include <iostream>
#include <queue>
#include <string>
#include "BlockQueue.hpp"
#include "Task.hpp"

void* productor(void *argc)
{
    //BlockQueue<Task>* bq = static_cast<BlockQueue<Task>*> (argc);
    BlockQueue<task_t>* bq = static_cast<BlockQueue<task_t>*> (argc);
    
    int x = 1, y = 1;
    while(true)
    {
        sleep(1);
        //Task t(1, 2);
        //sleep(1);
        //std::cout << "生产了一个任务: " << x << "+" << y << "=?" << std::endl;
        //Task t(x, y);
        //task_t t;
        std::cout << "生产了任务 download" << std::endl;
        bq->Equeue(Download);

        //x++, y++;
    }
    return nullptr;
}

void* consumer(void *argc)
{
    //BlockQueue<Task>* bq = static_cast<BlockQueue<Task>*> (argc);
    BlockQueue<task_t>* bq = static_cast<BlockQueue<task_t>*> (argc);
    
    while (true)
    {
        //sleep(1);
        //Task t = bq->Pop();
        task_t t = bq->Pop();
        t();

        //t.Execute();
        //std::cout << "*********" << std::endl;
        //std::cout << "消费了一个任务: " << t.X() << "+" << t.Y() << "=" << t.Result()  << std::endl;
    }
    return nullptr;
}

int main()
{
    //BlockQueue<Task>* q = new BlockQueue<Task>();
    BlockQueue<task_t>* q = new BlockQueue<task_t>();
    
    pthread_t ptid, ctid;

    pthread_create(&ptid, nullptr, productor, q);
    pthread_create(&ctid, nullptr, consumer, q);

    pthread_join(ptid, nullptr);
    pthread_join(ctid, nullptr);

    return 0;
}